/* #ifdef APP-PLUS */
import { showToast } from '@/utils/dialog'
/* #endif */

/**
 * 消息提示
 */
export function toast(options) {
  let config = {
    title: '',
    icon: 'none',
    image: '',
    mask: true,
    duration: 1200,
  }
  config = Object.assign(config, options)

  if (typeof options === 'string') {
    config.title = options
  }
  /* #ifdef APP-PLUS */
  showToast(config)
  /* #endif */
  /* #ifndef APP-PLUS */
  uni.showToast({
    title: config.title,
    icon: config.icon,
    image: config.image,
    mask: config.mask,
    duration: config.duration,
  })
  /* #endif */
}

/**
 * decodeURIComponent加密前处理
 */
export function encodeReplaceKey(key) {
  const encodeArr = [
    {
      code: '%',
      encode: '%25',
    },
    {
      code: '?',
      encode: '%3F',
    },
    {
      code: '#',
      encode: '%23',
    },
    {
      code: '&',
      encode: '%26',
    },
    {
      code: '=',
      encode: '%3D',
    },
  ]
  return key.toString().replace(/[%?#&=]/g, ($) => {
    for (const k of encodeArr) {
      if (k.code === $) {
        return k.encode
      }
    }
  })
}
